//
//  MSSearchDataSource.m
//  MSJD
//
//  Created by 箭军刘 on 2018/10/14.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "MSSearchDataSource.h"

@implementation MSSearchDataSource

+ (void)querySearchRecommondDataWithKind:(NSInteger)kind
                                 success:(void (^)(NSDictionary *responseInfo))success
                                 failure:(void (^)(NSError *error))failure {
    NSString *requestUrl = @"v1/search/hotwords";
    NSDictionary *param = @{@"kid":@(kind)};
    [MSNetworkAPI GET:requestUrl domain:API_TOPHRMING parameters:param success:^(id responseObject) {
        if (success) {
            success((NSDictionary *)responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

+ (void)searchContentListWithFilterParam:(NSDictionary *)filterParam
                                    page:(NSInteger)page
                                    size:(NSInteger)size
                                 success:(void (^)(NSDictionary *responseInfo))success
                                 failure:(void (^)(NSError *error))failure {
    NSString *requestUlr = @"v1/item/search";
    CGFloat lon = [[UserAccesser loadAccount].lon floatValue];
    CGFloat lat = [[UserAccesser loadAccount].lat floatValue];
    NSMutableDictionary *tempParam = [NSMutableDictionary dictionaryWithDictionary:filterParam];
    [tempParam addEntriesFromDictionary:@{@"page":@(page), @"size":@(size), @"lon":@(lon), @"lat":@(lat)}];
    [MSNetworkAPI POST:requestUlr domain:API_TOPHRMING parameters:tempParam.copy isJsonData:NO success:^(id responseObject) {
        if (success) {
            success((NSDictionary *)responseObject);
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}

#pragma mark - 本地操作

NSString *const kMGMLocalSearchListKey = @"kMGMLocalSearchListKey";
/**
 获取搜索历史列表
 */
+ (NSArray *)searchHistoryList {
    NSArray *localSearchList = [[NSUserDefaults standardUserDefaults] objectForKey:kMGMLocalSearchListKey];
    return [MSSafeObject ArrayInfo:localSearchList];
}

/**
 保存搜索内容至本地
 */
+ (void)saveSearchContent:(NSString *)content {
    if ([MSSafeObject isNull:content]) {
        return;
    }
    NSArray *localSearchList = [[NSUserDefaults standardUserDefaults] objectForKey:kMGMLocalSearchListKey];
    NSMutableArray *tempLocalSearchList = [NSMutableArray arrayWithArray:[MSSafeObject ArrayInfo:localSearchList]];
    if ([tempLocalSearchList containsObject:content]) {
        [tempLocalSearchList removeObject:content];
    }
    [tempLocalSearchList insertObject:content atIndex:0];
    if (tempLocalSearchList.count > 20) {
        [tempLocalSearchList removeLastObject];
    }
    [[NSUserDefaults standardUserDefaults] setObject:tempLocalSearchList.copy forKey:kMGMLocalSearchListKey];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 移除本地的搜索内容
 */
+ (void)removeSearchContent:(NSString *)content {
    NSArray *localSearchList = [[NSUserDefaults standardUserDefaults] objectForKey:kMGMLocalSearchListKey];
    NSMutableArray *tempLocalSearchList = [NSMutableArray arrayWithArray:[MSSafeObject ArrayInfo:localSearchList]];
    if ([tempLocalSearchList containsObject:content]) {
        [tempLocalSearchList removeObject:content];
        [[NSUserDefaults standardUserDefaults] setObject:tempLocalSearchList.copy forKey:kMGMLocalSearchListKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

/**
 清理所有的本地搜索内容列表
 */
+ (void)clearAllLocalSearchList {
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:kMGMLocalSearchListKey];
}


@end
